<template>
  <div>
    <div class="margin-container-list">
      <div class="pad-b-20 pad-t-20">
        <el-button type="primary" @click="saveBrand()">新增品牌</el-button>
      </div>
      <div class="top-operate-default">
        <div class="search-item">
          <label>搜索品牌：</label>
          <el-input @keyup.enter.native="handleSearch" v-model="keyworkedName" placeholder="请输入品牌名称" style="width: 300px; margin-right: 10px"></el-input>
          <label class="mar-l-20">品牌分类：</label>
          <el-select v-model="brandCategoryId" placeholder="请选择">
            <el-option
              v-for="item in listCategory"
              :key="item.brandCategoryId"
              :label="item.brandCategoryName"
              :value="item.brandCategoryId">
            </el-option>
          </el-select>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch" style="margin-left: 10px">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container mar-t-20">
        <el-table ref="multipleTable" :data="entityList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%">
          <el-table-column label="品牌">
            <template slot-scope="scope">
              <div class="flex cursor-p" style="margin-right: 30px;" @click="saveBrand(scope.row)">
                <img :src="scope.row.brandLogo" class="user-avatar">
                <div class="font-14 pad-l-10 v-align-h">
                  <div class="line-2" style="color:#155bd4;">
                    {{scope.row.brandName}}
                  </div>
                  <div class="font-color-6 font-13">[{{scope.row.brandCategoryName}}]</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="品牌介绍" prop="brandIntroduce"></el-table-column>
          <el-table-column label="商品" prop="goodsNum"></el-table-column>
          <el-table-column width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="saveBrand(scope.row)">编辑</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="delBrand(scope.row)"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange"
           @current-change="handleCurrentChange" :current-page="entityListQuery.currentPage"
           :page-sizes="[10, 20, 50, 100]" :page-size="entityListQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!--新增-->
    <el-dialog title="" :visible.sync="entityVisible" width="650px">
      <el-form ref="form" :model="entity" :rules="rules" label-position="right" label-width="100px" style='width: 500px; margin-left:50px;'>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="entity.brandName" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="品牌分类" prop="brandCategoryId">
          <el-select v-model="entity.brandCategoryId" placeholder="请选择">
            <el-option
              v-for="item in listCategory"
              :key="item.brandCategoryId"
              :label="item.brandCategoryName"
              :value="item.brandCategoryId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌介绍">
          <el-input type="textarea" :rows="6" placeholder="请输入" v-model="entity.brandIntroduce"></el-input>
        </el-form-item>
        <el-form-item label="品牌Logo">
          <div class="flex height-148 upload-remove-bar">
            <el-upload list-type="picture-card"
                       action="/api/common/uploadFiles"
                       accept="image/jpeg,image/gif,image/png,image/bmp"
                       name="files" multiple
                       :data="token"
                       :show-file-list="false"
                       v-loading="shareFileLoading"
                       :on-success="handleUploadShareImageUrlSuccess"
                       :before-upload="beforeShareImageUrlUpload">

              <img v-if="entity.brandLogo" :src="entity.brandLogo" style="width: 150px;height: 150px">

              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="close-icon width-36 height-36 flex flex-center cursor-p" @click="closeEls()">
              <svg-icon v-if="entity.brandLogo" icon-class="close" class="icon font-16 noraml-hide active" />
            </div>
          </div>
          <div class="tips mar-t-3">建议尺寸：750*750像素</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveEntity">确认</el-button>
        <el-button @click="entityVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { pageBrand, saveBrand, listBrandCategory, delBrand } from '@/api/user/enterpriseBrand'
  import { getToken } from '@/utils/auth'
  export default {
    data() {
      return {
        brandCategoryId: '',
        entityList: [],
        total: null,
        keyworkedName: '',
        entityListQuery: {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        },
        listCategory: [],
        listLoading: false,
        shareFileLoading: false,
        entity: {
          brandId: '',
          brandCategoryId: '',
          enterpriseId: this.getEnterpriseId(),
          brandName: '',
          brandLogo: '',
          brandIntroduce: ''
        },
        entityVisible: false,
        rules: {
          brandCategoryId: [
            { required: true, message: '请选择分类', trigger: 'blur' }
          ],
          brandName: [
            { required: true, message: '请填写品牌名称', trigger: 'blur' },
            { max: 32, message: '品牌名称最多输入32个字符' }
          ]
        }
      }
    },
    created() {
      this.token = { accessToken: getToken() }
      this.listBrandCategory()
      this.initPage()
    },
    methods: {
      handleSearch() {
        this.initPage()
      },
      handleReset() {
        this.entityListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        }
        this.brandCategoryId = ''
        this.keyworkedName = ''
        this.initPage()
      },
      listBrandCategory() {
        listBrandCategory(this.getEnterpriseId()).then(response => {
          if (response.isError) return
          this.listCategory = response.data
        })
      },
      delBrand(row) {
        delBrand(row.brandId).then(response => {
          if (response.isError) return
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.initPage()
        })
      },
      saveBrand(row) {
        this.entity = {}
        if (row) {
          this.entity = JSON.parse(JSON.stringify(row))
        }
        this.entityVisible = true
      },
      saveEntity() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.entity.enterpriseId = this.getEnterpriseId()
            saveBrand(this.entity).then(response => {
              if (response.isError) return
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.initPage()
              this.entityVisible = false
            })
          }
        })
      },
      initPage() {
        this.listLoading = true
        this.entityListQuery['brandName'] = this.keyworkedName
        this.entityListQuery['brandCategoryId'] = this.brandCategoryId
        pageBrand(this.entityListQuery).then(response => {
          if (response.isError) return
          this.entityList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.entityListQuery.currentPage = val
        this.initPage()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.entityListQuery.size = val
        this.initPage()
      },
      // 勾选
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      closeEls() {
        this.entity.brandLogo = ''
      },
      beforeShareImageUrlUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isGIF = file.type === 'image/gif'
        const isPNG = file.type === 'image/png'
        const isBMP = file.type === 'image/bmp'

        if (!isJPG && !isGIF && !isPNG && !isBMP) {
          this.$message.error('上传图片必须是JPG/GIF/PNG/BMP 格式!')
        }
        this.shareFileLoading = true
        return (isJPG || isBMP || isGIF || isPNG)
      },
      handleUploadShareImageUrlSuccess(res, file) {
        this.shareFileLoading = false
        if (res.code === 101) {
          const imageUrl = res.data[0]
          this.entity.brandLogo = imageUrl
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
          return false
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
  }
  .v-align-h{
    justify-content: space-between;
    display: flex;
    flex-direction: column;
  }
</style>
